package com.eight.cloud.security.utils;

import cn.hutool.core.util.ObjectUtil;
import com.eight.cloud.core.global.model.LoginUser;
import com.eight.cloud.core.utils.format.ObjectFormat;
import com.eight.cloud.redisson.utils.RedisUtils;
import com.eight.cloud.redisson.utils.TokenManager;
import org.springframework.security.authentication.AccountExpiredException;

/**
 * @ClassName: AuthUtils
 * @Author: TXC
 * @Date: 2024-10-18 10:17
 **/
public class AuthUtils {
    public static LoginUser getLoginUser(TokenManager tokenManager, RedisUtils redisUtils, String token) {
        try {
            String userId = tokenManager.getUserId(token);
            Object userInfo = redisUtils.get(tokenManager.getUserinfoCacheKey() + userId);
            if (ObjectUtil.isNotNull(userInfo)) {
                return ObjectFormat.objToObject(userInfo, LoginUser.class);
            }
            throw new AccountExpiredException("登陆已过期");
        }catch (Exception e) {
            throw new AccountExpiredException("登陆已过期");
        }
    }
}
